#!/bin/bash

usage() {
	echo "
usage: ${0##*/} [options] set

set menu for many desktop environments and applications
using the generated ${SYSROOT}/etc/xdg/menus/os.menu

options:
  -sysroot <path>: set system root
"
	exit
}

MENUS='
applications.menu
cinnamon-applications.menu
gnome-applications.menu
gnome-flashback-applications.menu
kde4-applications.menu
kf5-applications.menu
lxde-applications.menu
lxlauncher-applications.menu
lxqt-applications.menu
mate-applications.menu
moblin-applications.menu
xfce-applications.menu
'

DOT_DIRECTORY='
Office-Business-calc|Calculator|calculator.svg|
Business|Business|calculator.svg|
Office-Business-finance|Finance|calculator.svg|
Office-Business-planner|Planner|calculator.svg|
Office-Business-spreadsheet|Spreadsheet|calculator.svg|
Office-Business-Sub|Business|calculator.svg|
Desktop-appearance|Appearance|desktop.svg|
Desktop-applet|Applet|desktop.svg|
Desktop|Desktop|desktop.svg|
Desktop-settings|Settings|desktop.svg|
Desktop-sleep|Sleep|desktop.svg|
Desktop-Sub|Desktop|desktop.svg|
Desktop-windows|Windows|desktop.svg|
Office-Document-catalog|Catalog|write.svg|
Document|Document|write.svg|
Office-Document-edit|Edit|write.svg|
Office-Document-layout|Layout|write.svg|
Office-Document-Sub|Document|write.svg|
Office-Document-utility|Utility|write.svg|
Office-Document-viewer|Viewer|write.svg|
Filesystem|Filesystem|directory.svg|
Filesystem-filemanager|Filemanager|directory.svg|
Filesystem-find|Find|directory.svg|
Filesystem-mount|Mount|directory.svg|
Filesystem-storage|Storage|directory.svg|
Filesystem-Sub|Filesystem|directory.svg|
Fun-adventure|Adventure|game.svg|
Fun-arcade|Arcade|game.svg|
Fun-boardgame|Board game|game.svg|
Fun-cardgame|Card game|game.svg|
Fun|Fun|game.svg|
Fun-puzzle|Puzzle|game.svg|
Fun-shooter|Shooter|game.svg|
Fun-Sub|Fun|game.svg|
Graphic-camera|camera|image.svg|
Graphic|Graphic|image.svg|
Graphic-draw|Draw|image.svg|
Graphic-flow|Flow|image.svg|
Graphic-paint|Paint|image.svg|
Graphic-print|Print|image.svg|
Graphic-scanner|Scanner|image.svg|
Graphic-Sub|Graphic|image.svg|
Graphic-utility|Utility|image.svg|
Graphic-viewer|Viewer|image.svg|
Internet-block|Block|internet.svg|
Internet-browser|Browser|internet.svg|
Internet-chat|Chat|internet.svg|
Internet|Internet|internet.svg|
Internet-login|Login|internet.svg|
Internet-mailnews|Mail and News|internet.svg|
Internet-Sub|Internet|internet.svg|
Internet-transfer|Transfer|internet.svg|
Internet-utility|Utility|internet.svg|
Multimedia-audiovideo|Audio and Video|multimedia.svg|
Multimedia|Multimedia|multimedia.svg|
Multimedia-mediaplayer|Player|multimedia.svg|
Multimedia-optical|Optical|multimedia.svg|
Multimedia-sound|Audio|multimedia.svg|
Multimedia-Sub|Multimedia|multimedia.svg|
Multimedia-utility|Utility|multimedia.svg|
Network-connect|Connect|network2.svg|
Network|Network|network2.svg|
Network-firewall|Firewall|network2.svg|
Network-phone|Phone|network2.svg|
Network-Sub|Network|network2.svg|
Network-transfer|Transfer|network2.svg|
Network-utility|Utility|network2.svg|
Office|Office|spreadsheet.svg|
Personal-database|Database|user.svg|
Personal-date|Date|user.svg|
Personal|Personal|user.svg|
Personal-education|Education|user.svg|
Personal-Sub|Personal|user.svg|
Personal-timing|Timing|user.svg|
Setup|Setup|preferences.svg|
Setup-installation|Installation|preferences.svg|
Setup-puppy|Puppy|preferences.svg|
Setup-Sub|Setup|preferences.svg|
Setup-wizard|Wizard|preferences.svg|
System|System|preferences.svg|
System-memory|Memory|preferences.svg|
System-print|Print|preferences.svg|
System-process|Process|preferences.svg|
System-security|Security|preferences.svg|
System-storage|Storage|preferences.svg|
System-Sub|System|preferences.svg|
Utility-development|Development|building_block.svg|
Utility|Utility|building_block.svg|
Utility-help|Help|building_block.svg|
Utility-package|Package|building_block.svg|
Utility-shell|Shell|building_block.svg|
Utility-Sub|Utility|building_block.svg|
'

CATEGORIES='
Desktop
System
Setup
<Separator/>
Utility
Filesystem
Graphic
Office
Personal
Network
Internet
Multimedia
Fun
<Separator/>
Help
'

SUBCATEGORIES='
Desktop-Sub:X-Desktop
Desktop-appearance:X-Desktop-appearance,DesktopSettings
Desktop-settings:X-Desktop-settings,X-DesktopCountry,Accessibility
Desktop-windows:X-Desktop-windows
Desktop-sleep:X-Desktop-sleep,Screensaver
Desktop-applet:X-Desktop-applet,X-DesktopApplets,Clock
System-Sub:X-System,System,HardwareSettings,Core
System-process:X-System-process,Monitor,X-SystemSchedule
System-memory:X-System-memory,X-SystemMemory
System-print:X-System-print
System-storage:X-System-storage
System-security:Security,X-System-security
Setup-Sub:X-Setup
Setup-puppy:X-Setup-puppy,X-SetupEntry,PackageManager
Setup-wizard:X-Setup-wizard,X-SetupWizard
Setup-installation:X-Setup-installation,X-SetupUtility
Utility-Sub:X-Utility,Utility,Viewer,Electronics,Emulator,Engineering
Utility-development:X-Utility-development,Development,Building,Debugger,IDE,Profiling,ProjectManagement,RevisionControl,Translation,GUIDesigner
Utility-package:X-Utility-package,Archiving,Compression
Utility-shell:X-Utility-shell,TerminalEmulator,Shell
Utility-help:X-Utility-help,Documentation
Filesystem-Sub:X-Filesystem,FileSystem,FileTools
Filesystem-filemanager:X-Filesystem-filemanager,FileManager
Filesystem-mount:X-Filesystem-mount,X-FilesystemMount
Filesystem-find:X-Filesystem-find,X-FilesystemFind
Filesystem-storage:X-Filesystem-storage,X-FilesystemUtility
Graphic-Sub:X-Graphic,2DGraphics,3DGraphics
Graphic-paint:X-Graphic-paint,RasterGraphics
Graphic-draw:X-Graphic-draw,VectorGraphics
Graphic-flow:X-Graphic-flow,Chart,FlowChart,Presentation
Graphic-viewer:X-Graphic-viewer,Viewer
Graphic-camera:X-Graphic-camera,Photography
Graphic-scanner:X-Graphic-scanner,Scanning,OCR
Graphic-print:X-Graphic-print
Graphic-utility:X-Graphic-utility,X-GraphicUtility
Office-Document-Sub:X-Document
Office-Document-layout:X-Document-layout,WordProcessor,WebDevelopment,Publishing
Office-Document-catalog:X-Document-catalog,Dictionary
Office-Document-edit:X-Document-edit,TextEditor
Office-Document-viewer:X-Document-viewer,Viewer
Office-Document-utility:X-Document-utility,X-DocumentUtility
Office-Business-Sub:X-Business,X-Calculate
Office-Business-spreadsheet:X-Business-spreadsheet,Spreadsheet
Office-Business-planner:X-Business-planner,ProjectManagement
Office-Business-finance:X-Business-finance,Finance
Office-Business-calc:X-Business-calc,Calculator,X-CalculateUtility
Personal-Sub:X-Personal
Personal-database:X-Personal-database,ContactManagement
Personal-date:X-Personal-date,Calendar
Personal-organizer:X-Personal-organizer,PDA
Personal-security:X-Personal-security
Personal-timing:X-Personal-timing,X-PersonalUtility
Personal-education:X-Personal-education,Languages,Science,ArtificialIntelligence,Astronomy,Biology,Chemistry,ComputerScience,DataVisualization,Economy,Electricity,Geography,Geology,Geoscience,History,ImageProcessing,Literature,Math,NumericalAnalysis,MedicalSoftware,Physics,Robotics,Sports,ParallelComputing
Network-Sub:X-Network,Network
Network-firewall:X-Network-firewall
Network-phone:X-Network-phone,Dialup
Network-connect:X-Network-connect,HamRadio,RemoteAccess
Network-transfer:X-Network-transfer
Network-utility:X-Network-utility
Internet-Sub:X-Internet
Internet-browser:X-Internet-browser,WebBrowser
Internet-chat:X-Internet-chat,InstantMessaging,Chat,VideoConference
Internet-mailnews:X-Internet-mailnews,Email,News
Internet-transfer:X-Internet-transfer,Telephony,IRCClient,FileTransfer,P2P
Internet-login:X-Internet-login
Internet-block:X-Internet-block
Internet-utility:X-Internet-utility
Multimedia-Sub:X-Multimedia
Multimedia-mediaplayer:X-Multimedia-mediaplayer
Multimedia-audiovideo:X-Multimedia-audiovideo,AudioVideo,AudioVideoEditing,Tuner
Multimedia-sound:X-Multimedia-sound,Music,Audio,Midi,Mixer
Multimedia-video:X-Multimedia-video,Video
Multimedia-optical:X-Multimedia-optical,DiskBurning
Multimedia-utility:X-Multimedia-utility
Fun-Sub:X-Fun,Game,Amusement,BlocksGame,KidsGame,Simulation,SportsGame
Fun-adventure:X-Fun-adventure,ActionGame,AdventureGame,RolePlaying
Fun-boardgame:X-Fun-boardgame,BoardGame
Fun-cardgame:X-Fun-cardgame,CardGame
Fun-puzzle:X-Fun-puzzle,StrategyGame,LogicGame
Fun-shooter:X-Fun-shooter
Fun-arcade:X-Fun-arcade,ArcadeGame
'

#===========================================================

[ -z "$1" ] && usage

for i in $@ ; do
	case $1 in
		-prefix|-sysroot) SYSROOT="$2" ; shift 2 ;;
		set|-set) PSET=1 ;;
		*) usage ;;
	esac
done

[ "$PSET" != "1" ] && exit

#=====================================================
#            /etc/xdg/menus/os.menu
#=====================================================

mkdir -p ${SYSROOT}/etc/xdg/menus
rm -f ${SYSROOT}/etc/xdg/menus/os.menu

cat > ${SYSROOT}/etc/xdg/menus/os.menu <<_EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"
 "http://www.freedesktop.org/standards/menu-spec/1.0/menu.dtd">

<Menu>
	<Name>Applications</Name>
	<DefaultAppDirs/>
	<DefaultDirectoryDirs/>
	<DefaultMergeDirs/>
	<Layout>
_EOF

(
for i in $CATEGORIES ; do
	case $i in
		*Separator*) echo "		$i" ;;
		*) echo "		<Menuname>${i}</Menuname>" ;;
	esac
done
echo "	</Layout>"
) >> ${SYSROOT}/etc/xdg/menus/os.menu

CATEGORIES=$(echo "$CATEGORIES" | sed '/^$/d ; /Separator/d')

(
for i in $CATEGORIES ; do
	echo
	echo
	SUBC=$(echo "$SUBCATEGORIES" | grep "^${i}-")
	if [ -z "$SUBC" ] ; then
		continue
	fi
	echo '	<!-- ==================================== -->'
	echo "	<Menu>"
	echo "		<Name>${i}</Name>"
	echo "		<Directory>${i}.directory</Directory>"
	echo "		<Layout>"
	for x in $SUBC ; do
		IFS=":" read name zzz <<< "$x"
		echo '			<Menuname inline="true" inline_limit="99">'${name}'</Menuname>'
		echo '			<Separator/>'
	done
	echo "		</Layout>"
	for x in $SUBC ; do
		IFS=":" read name includes <<< "$x"
		echo
		echo "		<Menu>"
		echo "			<Name>${name}</Name>"
		echo "			<Directory>${name}.directory</Directory>"
		echo "			<Include>"
		for include in ${includes//,/ } ; do
			echo "				<Category>${include}</Category>"
		done
		echo "			</Include>"
		echo "		</Menu>"
	done
	echo "	</Menu>"
done
echo "</Menu>"
) >> ${SYSROOT}/etc/xdg/menus/os.menu

#=====================================================
#   /usr/share/desktop-directories/*.directory
#=====================================================

mkdir -p ${SYSROOT}/usr/share/desktop-directories
while IFS="|" read FILE DESC ICON ZZZ
do
	[ -z "$FILE" ] && continue
	echo $FILE
	(
	printf "[Desktop Entry]\n"
	printf "Encoding=UTF-8\n"
	printf "Name=${DESC}\n"
	printf "Icon=/usr/share/pixmaps/puppy/${ICON}\n"
	printf "Type=Directory\n"
	) > ${SYSROOT}/usr/share/desktop-directories/${FILE}.directory
done <<EOF
$DOT_DIRECTORY
EOF

#=====================================================
#            /etc/xdg/menus/*.menu
#=====================================================

if [ -f ${SYSROOT}/etc/xdg/menus/os.menu ] ; then
	cd ${SYSROOT}/etc/xdg/menus
	for i in $MENUS ; do
		rm -f $i
		ln -sv os.menu $i
	done
else
	echo "ERROR: ${SYSROOT}/etc/xdg/menus/os.menu not found..."
	exit 1
fi

### END ###